<template>
  <div class="app-container">
    <div class="filter-container clearfix">
      <el-button v-if="checkPermission(permissionRoles.monitor_rule_add)" class="filter-item" type="primary" icon="el-icon-plus" @click="handleAdd">添加</el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border stripe style="width: 100%;" height="670">
      <el-table-column label="规则ID" prop="id" align="center"></el-table-column>
      <el-table-column label="规则分类" align="center">
        <template slot-scope="{row}">
          <span v-if="row.rule_type === 1">关键词</span>
          <span v-else-if="row.rule_type === 2">连续发言</span>
          <span v-else>私聊人数</span>
        </template>
      </el-table-column>
      <el-table-column label="大区" prop="srname" width="160px" align="center"></el-table-column>
      <el-table-column label="开服天数" prop="open_days" width="160px" align="center"></el-table-column>
      <el-table-column label="指定时间(秒)" prop="specify_time" align="center"></el-table-column>
      <el-table-column label="次数/人数" prop="number" align="center"></el-table-column>
      <el-table-column label="惩罚方式" align="center">
        <template slot-scope="{row}">
          <span v-if="row.punish_type === 1">禁言</span>
          <span v-else-if="row.punish_type === 2">封账号</span>
          <span v-else>无感禁言</span>
        </template>
      </el-table-column>
      <el-table-column label="惩罚时间(秒)" prop="punish_time" align="center"></el-table-column>
      <el-table-column label="添加时间" prop="create_dt" width="140px" align="center"></el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button v-if="checkPermission(permissionRoles.monitor_rule_del)" type="text" size="small" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogVisible" :title="textMap[dialogType]" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" :rules="rules" label-width="100px" label-position="left">
        <el-form-item label="大区" prop="srid">
          <el-select v-model="dataForm.srid" clearable class="filter-item" style="width: 120px;" placeholder="大区">
            <el-option
              v-for="(item, index) in serverRegionList"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="规则类型" prop="ruleType">
          <el-select v-model="dataForm.ruleType" clearable class="filter-item" style="width: 120px;" placeholder="选择规则类型">
            <el-option label="关键词" value="1" />
            <el-option label="连续发言" value="2" />
            <el-option label="私聊人数" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="开服天数" prop="openDays">
          <el-input v-model="dataForm.openDays" class="defaut-input" placeholder="填写开服天数" />
        </el-form-item>
        <el-form-item label="指定时间" prop="specifyTime">
          <el-input v-model="dataForm.specifyTime" class="defaut-input" placeholder="填写指定时间，单位：秒" />
        </el-form-item>
        <el-form-item label="发言次数" prop="number">
          <el-input v-model="dataForm.number" class="defaut-input" placeholder="填写发言次数" />
        </el-form-item>
        <el-form-item label="惩罚方式" prop="punishType">
          <el-select v-model="dataForm.punishType" clearable class="filter-item" style="width: 120px;" placeholder="选择惩罚方式">
            <el-option label="禁言" value="1" />
            <el-option label="封账号" value="2" />
            <el-option label="无感禁言" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="惩罚时间" prop="punishTime">
          <el-input v-model="dataForm.punishTime" class="defaut-input" placeholder="填写惩罚时间，单位：秒" />
        </el-form-item>
      </el-form>
      <div class="tr">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确定</el-button>
      </div>
    </el-dialog>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission' // 权限判断函数
import waves from '@/directive/waves' // 实现点击波浪纹
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { getServerRegionList } from '@/api/ops'
import { handleMonitorRule, getMonitorRuleList, deleteMonitorRule } from '@/api/chat'

const defaultItem = {
  id: '',
  ruleType: '',
  srid: '',
  openDays: '',
  specifyTime: '',
  number: '',
  punishType: '',
  punishTime: ''
}
export default {
  name: 'MonitorRule',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      list: [],
      total: 0,
      listLoading: false,
      dataForm: Object.assign({}, defaultItem),
      serverRegionList: [],
      rules: {
        ruleType: [{ required: true, message: '请选择规则类型', trigger: 'blur' }],
        srid: [{ required: true, message: '请选择大区', trigger: 'blur' }],
        openDays: [{ required: true, message: '请填写开服天数', trigger: 'blur' }],
        specifyTime: [{ required: true, message: '请填写指定时间', trigger: 'blur' }],
        number: [{ required: true, message: '请填写次数/人数', trigger: 'blur' }],
        punishType: [{ required: true, message: '请选择惩罚类型', trigger: 'blur' }],
        punishTime: [{ required: true, message: '请填写惩罚时间', trigger: 'blur' }]
      },
      listQuery: {
        page: 1,
        limit: 200
      },
      dialogVisible: false,
      dialogType: 'new',
      textMap: {
        new: '新增'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission,
    async getList() {
      const query = this.listQuery
      this.listLoading = true
      const res = await getMonitorRuleList(query)
      this.listLoading = false
      this.list = res.data.items
      this.total = res.data.total
    },
    handleFilter() {
      this.getList()
    },
    async getServerRegionList() {
      const res = await getServerRegionList()
      this.serverRegionList = res.data
    },
    handleAdd() {
      this.getServerRegionList()
      this.dataForm = Object.assign({}, defaultItem)
      this.dialogType = 'new'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    confirmSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.dataForm.sid = this.selectedData
          handleMonitorRule(this.dataForm).then(response => {
            if (response.status) {
              this.dialogVisible = false
              this.getList()
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
            } else {
              this.message.error(response.message)
            }
          })
        }
      })
    },
    handleDelete({ $index, row }) {
      this.$confirm('你确定要删除此行数据吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteMonitorRule(row.id)
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(err => { console.error(err) })
    }
  }
}
</script>
